package inventoryservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/result"
)

type InventoryAssetCheckoutDetailAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInventoryAssetCheckoutDetailAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InventoryAssetCheckoutDetailAddLogic {
	return &InventoryAssetCheckoutDetailAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// InventoryAssetCheckoutDetailAdd 创建资产出库明细表项
func (l *InventoryAssetCheckoutDetailAddLogic) InventoryAssetCheckoutDetailAdd(in *property_service.InventoryAssetCheckoutDetail) (*property_service.Response, error) {
	// 获取资产出库明细表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).InventoryAssetCheckoutDetail
	// 创建一个新的资产出库明细表模型对象，不包含ID、TenantID、CreatedAt、UpdatedAt字段
	mod := &model.InventoryAssetCheckoutDetail{
		CheckoutID:      in.CheckoutID,      // 出库单ID
		AssetID:         in.AssetID,         // 资产ID
		AssetCode:       in.AssetCode,       // 资产编号
		AssetName:       in.AssetName,       // 资产名称
		Category:        in.Category,        // 分类
		CategoryID:      in.CategoryID,      // 分类ID
		SpecModel:       in.SpecModel,       // 规格型号
		SpecModelID:     in.SpecModelID,     // 规格型号ID
		Brand:           in.Brand,           // 品牌
		BrandID:         in.BrandID,         // 品牌ID
		Origin:          in.Origin,          // 产地
		OriginID:        in.OriginID,        // 产地ID
		Unit:            in.Unit,            // 单位
		UnitPrice:       in.UnitPrice,       // 单价
		Quantity:        in.Quantity,        // 数量
		StorageLocation: in.StorageLocation, // 存放地点
		Manufacturer:    in.Manufacturer,    // 生产商
		ManufacturerID:  in.ManufacturerID,  // 生产商ID
		TenantID:        in.TenantID,        // 租户ID
		CreatedAtuser:   in.CreatedAtuser,   // 创建人
		UpdatedAtuser:   in.UpdatedAtuser,   // 更新人
	}
	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		// 如果创建失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 创建成功，返回成功响应
	return result.Suc().AddEd()
}
